from django.shortcuts import render, HttpResponse
import json
from git.models import UserModel as User
from git.models import GoodModel as Good
from django.views.decorators.csrf import csrf_exempt


# Create your views here.


def index(request):
    return render(request, 'git/index.html')

@csrf_exempt
def login(request):
    if request.method == 'GET':
        return HttpResponse(json.dumps({'state': 500, 'message': '请求方法错误'}), content_type="application/json")
    username = request.POST['username']
    password = request.POST['password']

    # User.objects.create(username="admin1", password="123456")
    # for i in range(0, 5):
    #     Good.objects.create(name='good'+str(i), price='12.0'+str(i), user=User.objects.get(id=1))
    # if username is None or password is None:
    #     return HttpResponse(json.dumps({'state': 501, 'message': '参数错误'}), content_type="application/json")
    # users = User.objects.filter(username=username, password=password).values('id', 'username', 'status')
    # if len(users) == 0:
    #     return HttpResponse(json.dumps({'state': 502, 'message': '用户名或密码错误'}), content_type="application/json")
    # print(users[0])
    # Good.objects.create(name='good2', price='12.02',user=User.objects.get(id=1))
    # test = Good.objects.all().values('id', 'name', 'price')
    # print(list(test))
    # good1 = User.objects.select_related('').all().values('username')
    # print(good1.query)
    # print(good1)
    test_query_user()
    test_query_good()
    resp = {
        'state': 200,
        'message': '',
        'data': {}
    }
    return HttpResponse(json.dumps(resp), content_type="application/json")


def test_query_user():
    users = User.objects.all()
    for user in users:
        print(user)
    print(users.query)


def test_query_good():
    goods = Good.objects.all()
    for g in goods:
        print(g.user)
